const process = require('child_process');
const json = require('./package.json')

const dependencies = Object.keys(json.dependencies)
const devDependencies = Object.keys(json.devDependencies)

const install = function (array, isDev, complete) {
  if (array.length > 0) {
    const name = array.shift()
    console.log(`install ${name}`)
    process.exec(`npm update ${name} --save${isDev ? '-dev' : ''}`, function (err) {
      if (err) {
        console.error(`install ${name} fail!`)
        return
      }
      console.log(`install ${name} completed!`)
      install(array, isDev, complete)
    })
  } else {
    complete && complete()
  }
}

install(dependencies, false, function () {
  console.log('-------- completed! ---------')
  // install(devDependencies, true, function(){
  //   console.log('-------- completed! ---------')
  // })
})
